package mobidever.mygodutch.database.sqlitedal;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import mobidever.mygodutch.database.base.SQLiteDALBase;
import mobidever.mygodutch.model.ModelPayout;
import mobidever.mygodutch.utility.DateTools;

public class SQLiteDALPayout extends SQLiteDALBase
{

	public SQLiteDALPayout(Context pContext)
	{
		super(pContext);
	}

	public boolean insertPayout(ModelPayout pModelPayout)
	{
		ContentValues _ContentValues = modelPayoutChangeToContentValues(pModelPayout);
		long _ID = getDatabase().insert(getTableNameAndPK()[0], null,
				_ContentValues);
		pModelPayout.setPayoutID(_ID);
		return _ID >= 0;
	}

	public boolean deletePayout(String pCondition)
	{
		return getDatabase().delete(getTableNameAndPK()[0], pCondition, null) > 0;
	}

	public boolean updatePayout(ModelPayout pModelPayout)
	{
		long _id = pModelPayout.getPayoutID();
		ContentValues _ContentValues = modelPayoutChangeToContentValues(pModelPayout);
		return getDatabase().update(getTableNameAndPK()[0], _ContentValues,
				"PayoutID=" + _id, null) > 0;
	}

	public boolean updatePayout(ModelPayout pModelPayout,
			String pCondition)
	{
		ContentValues _ContentValues = modelPayoutChangeToContentValues(pModelPayout);
		return getDatabase().update(getTableNameAndPK()[0], _ContentValues,
				"and PayoutID=" + pCondition, null) > 0;
	}
	
	public boolean updatePayout(ContentValues pContentValues,String pCondition)
	{
		return getDatabase().update(getTableNameAndPK()[0], pContentValues, pCondition, null) > 0;
	}

	public List<ModelPayout> getPayouts(String pCondtition)
	{
		String _sql = "select * from " + getTableNameAndPK()[0] + " where 1=1 "
				+ pCondtition;
		ArrayList<ModelPayout> _ArrayList = (ArrayList<ModelPayout>) getList(_sql);
		return _ArrayList;
	}

	private ContentValues modelPayoutChangeToContentValues(
			ModelPayout pModelPayout)
	{
		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("AccountBookID", pModelPayout.getAccountBookID());
		_ContentValues.put("CategoryID", pModelPayout.getCategoryID());
		_ContentValues.put("PayWayID",pModelPayout.getPayWayID());
		_ContentValues.put("PlaceID",pModelPayout.getPlaceID());
		_ContentValues.put("Amount",pModelPayout.getAmount().toString());
		_ContentValues.put("PayoutDate",DateTools.getFormatDateTime(pModelPayout.getPayoutDate(),"yyyy-MM-dd"));
		_ContentValues.put("PayoutType",pModelPayout.getPayoutType());
		_ContentValues.put("PayoutUserID",pModelPayout.getPayoutUserID());
		_ContentValues.put("Comment",pModelPayout.getComment());
		_ContentValues.put("CreateDate",DateTools.getFormatDateTime(pModelPayout.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
		_ContentValues.put("State",pModelPayout.getState());
		return _ContentValues;
	}

	@Override
	protected String[] getTableNameAndPK()
	{
		return new String[]
		{ "Payout", "PayoutID" };
	}

	@Override
	protected Object findModel(Cursor pCursor)
	{
		ModelPayout _ModelPayout = new ModelPayout();
		_ModelPayout.setPayoutID(pCursor.getInt(pCursor.getColumnIndex("PayoutID")));
		_ModelPayout.setAccountBookID(pCursor.getInt((pCursor.getColumnIndex("AccountBookID"))));
		//_ModelPayout.setAccountBookName(pCursor.getString(pCursor.getColumnIndex("AccountBookName")));
		_ModelPayout.setAccountBookName("");
		_ModelPayout.setCategoryID(pCursor.getInt((pCursor.getColumnIndex("CategoryID"))));
		//_ModelPayout.setCategoryName((pCursor.getString(pCursor.getColumnIndex("CategoryName"))));
		_ModelPayout.setCategoryName("");
		//_ModelPayout.setPath((pCursor.getString(pCursor.getColumnIndex("Path"))));
		_ModelPayout.setPath("");
		_ModelPayout.setPayWayID(pCursor.getInt((pCursor.getColumnIndex("PayWayID"))));
		_ModelPayout.setPlaceID(pCursor.getInt((pCursor.getColumnIndex("PlaceID"))));
		_ModelPayout.setAmount(new BigDecimal(pCursor.getString(((pCursor.getColumnIndex("Amount"))))));
		Date _PayoutDate = DateTools.getDate(pCursor.getString(pCursor.getColumnIndex("PayoutDate")), "yyyy-MM-dd");	
		_ModelPayout.setPayoutDate(_PayoutDate);
		_ModelPayout.setPayoutType((pCursor.getString(pCursor.getColumnIndex("PayoutType"))));
		_ModelPayout.setPayoutUserID((pCursor.getString(pCursor.getColumnIndex("PayoutUserID"))));
		_ModelPayout.setComment((pCursor.getString(pCursor.getColumnIndex("Comment"))));
		Date _CreateDate = DateTools.getDate(pCursor.getString(pCursor.getColumnIndex("CreateDate")), "yyyy-MM-dd HH:mm:ss");	
		_ModelPayout.setCreateDate(_CreateDate);
		_ModelPayout.setState((pCursor.getInt(pCursor.getColumnIndex("State"))));
		
		return _ModelPayout;
	}

	public void onCreate(SQLiteDatabase pDatabase)
	{
		StringBuilder sCreateTableScript = new StringBuilder();
		
		sCreateTableScript.append("		Create  TABLE Payout(");
		sCreateTableScript.append("				[PayoutID] integer PRIMARY KEY AUTOINCREMENT NOT NULL");
		sCreateTableScript.append("				,[AccountBookID] integer NOT NULL");
		sCreateTableScript.append("				,[CategoryID] integer NOT NULL");
		sCreateTableScript.append("				,[PayWayID] integer");
		sCreateTableScript.append("				,[PlaceID] integer");
		sCreateTableScript.append("				,[Amount] decimal NOT NULL");
		sCreateTableScript.append("				,[PayoutDate] datetime NOT NULL");
		sCreateTableScript.append("				,[PayoutType] varchar(20) NOT NULL");
		sCreateTableScript.append("				,[PayoutUserID] text NOT NULL");
		sCreateTableScript.append("				,[Comment] text");
		sCreateTableScript.append("				,[CreateDate] datetime NOT NULL");
		sCreateTableScript.append("				,[State] integer NOT NULL");
		sCreateTableScript.append("				)");

		pDatabase.execSQL(sCreateTableScript.toString());
	}

	public void onUpdate(SQLiteDatabase pDatabase)
	{
		// TODO Auto-generated method stub

	}

}
